package number_56;

public class Solution {
    public int[][] merge(int[][] intervals) {
        int n = intervals.length;
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (intervals[i][1] < intervals[j][0] || intervals[i][0] > intervals[j][1]) {
                    continue;
                } else {
                    intervals[j][0] = Math.min(intervals[i][0], intervals[j][0]);
                    intervals[j][1] = Math.max(intervals[i][1], intervals[j][1]);
                    intervals[i] = null;
                    count++;
                    break;
                }

            }
        }
        int[][] ret = new int[n - count][2];
        int r = 0;
        for (int i = 0; i < n; i++) {
            if (intervals[i] != null) {
                ret[r++] = intervals[i];
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        new Solution().merge(new int[][]{{1,3},{2,6},{8,10},{15,18}});
    }

}
